package com.cooge.xxmp.util;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class ConectionUtil {
	  
	
	
	public static	ConnectionConfiguration config = new ConnectionConfiguration(PropertyUtil.getContextProperty("serveaddress").toString(), Integer.valueOf(PropertyUtil.getContextProperty("serveport").toString()));
	
	public static XMPPConnection connection;
	
	public static XMPPConnection getConnection(){
		
		if(connection==null){
			createConnection();
		}
		return connection;
	}
	
	public static void  createConnection(){
		
		XMPPConnection connection = new XMPPConnection(config);
		try {
			connection.connect();
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ConectionUtil.connection = null;
		}
		try {
			
			String username = PropertyUtil.getContextProperty("servename").toString();
			
			String password = PropertyUtil.getContextProperty("servepassword").toString();
			
			connection.login(username, password);
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ConectionUtil.connection = null;
		}
		ConectionUtil.connection =  connection;
	}
	
}
